﻿using Microsoft.AspNetCore.Mvc.Filters;

namespace LS.Screening.Infrastructure
{
    /// <summary>
    /// 全局属性注入过滤器
    /// 实现Controller属性注入
    /// </summary>
    public class PropertyInjectionFilter : IActionFilter
    {
        private readonly IServiceProvider serviceProvider;

        public PropertyInjectionFilter(IServiceProvider serviceProvider)
        {
            this.serviceProvider = serviceProvider;
        }

        public void OnActionExecuting(ActionExecutingContext context)
        {
            // 获取控制器实例
            var controller = context.Controller;
            if (controller != null)
            {
                // 注入属性
                PropertyInjector.InjectProperties(controller, serviceProvider);
            }
        }

        public void OnActionExecuted(ActionExecutedContext context)
        {

        }
    }
}
